// pages/grid/visitdesc/index.js
import config from '../../../utils/config';
import { request } from '../../../utils/request';
import util from '../../../common/util';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.baseUrl,
        // 业务字典
        busiList: [],
        keynoteList: [],
        reservationBusiList: [],
        // 营销成功
        semResultList: [],
        // 有无
        hasnoList: [],
        // 商户信息
        merchantDetail: {},
        visitDetail: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getMerchantInfo(options.merchantId);
        this.getMerchantVisitInfo(options.visitId);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },


    // 查看图片
    previewImage(e) {
        let { url } = e.currentTarget.dataset;
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: [url] // 需要预览的图片http链接列表
        })
    },

    // 拜访信息
    getMerchantVisitInfo(id){
        request({
            url: "/merchantMarketing/getMerchantVisitInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: id
            }
        }).then(res => {
            if(res.code == 0){
                let visitDetail = res.data;
                visitDetail.reminderDate = util.formatDate(new Date(visitDetail.reminderDate));
                visitDetail.appointmentDate = visitDetail.appointmentDate?util.formatDate(new Date(visitDetail.appointmentDate)):"";
                this.setData({
                    visitDetail
                })
                // 获取字典
                this.getDictionaryInfo();
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    // 商户信息
    getMerchantInfo(id){
        request({
            url: "/merchantMarketing/getMerchantInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: id
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    merchantDetail: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    
    /**
     * 获取营销重点字典
     */
    getDictionaryInfo() {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
                dictType: "ecard_merchant_business_tag"
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    busiList: res.data
                })
                this.getKeynoteList(res.data);
            }
            return request({
                url: "/commonApi/getDictionaryInfo",
                method: "GET",
                data: {
                    dictType: "ecard_sem_result"
                }
            })
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    semResultList: res.data
                })
            }
            return request({
                url: "/commonApi/getDictionaryInfo",
                method: "GET",
                data: {
                    dictType: "ecard_has_reservation_busi"
                }
            })
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    hasnoList: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    // 获取营销重点
    getKeynoteList(list){
        let keynoteList = [];
        let reservationBusiList = [];
        let arr = this.data.visitDetail.newBusiness?this.data.visitDetail.newBusiness.split(","):[];
        let rarr = this.data.visitDetail.reservationBusiType?this.data.visitDetail.reservationBusiType.split(","):[];
        list.forEach(item => {
            if(arr.indexOf(item.dictValue)!=-1){
                keynoteList.push(item)
            }
            if(rarr.indexOf(item.dictValue)!=-1){
                reservationBusiList.push(item)
            }
        })
        this.setData({
            keynoteList,
            reservationBusiList
        }) 
    },
})